package dao.impl;

import dao.BaseDao;
import dao.MachineroomDao;
import entity.Computerroom;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MachineroomDaolmpl extends BaseDao implements MachineroomDao {
    //查询全部
    @Override
    public List<Computerroom> SelectAll() {
        List<Computerroom> list = new ArrayList<>();
        try{
            conn = super.getConnection();
            pstmt = conn.prepareStatement("select `roomId`, `roomName`, `computerNumber`, `manager`, `location`, `state` from computerroom");
            rs = pstmt.executeQuery();
            while (rs.next()){
                Computerroom computerroom = new Computerroom();
                computerroom.setRoomId(rs.getInt(1));
                computerroom.setRoomName(rs.getString(2));
                computerroom.setComputerNumber(rs.getInt(3));
                computerroom.setManager(rs.getString(4));
                computerroom.setLocation(rs.getString(5));
                computerroom.setState(rs.getInt(6));
                list.add(computerroom);
            }
        }catch (SQLException ex){
            ex.printStackTrace();
        }finally {
            closeAll(conn,pstmt,rs);
        }
        return list;
    }

    //删除
    @Override
    public int delete(int id) {
        int result = -1;
        try{
            conn = super.getConnection();
            pstmt = conn.prepareStatement("delete from `computerroom` where roomId = ?");
            pstmt.setInt(1,id);
            result = pstmt.executeUpdate();
        }catch (SQLException ex){
            ex.printStackTrace();
        }finally {
            closeAll(conn,pstmt,rs);
        }
        return result;
    }

    //新增
    @Override
    public int add(Computerroom computerroom) {
        int count=0;
        try{
            conn=super.getConnection();
            pstmt = conn.prepareStatement("INSERT INTO `computerroom`(`roomName`, `computerNumber`, `manager`, `location`, `state`) VALUES (?,?,?,?,?)");
            pstmt.setString(1,computerroom.getRoomName());
            pstmt.setInt(2,computerroom.getComputerNumber());
            pstmt.setString(3,computerroom.getManager());
            pstmt.setString(4,computerroom.getLocation());
            pstmt.setInt(5,computerroom.getState());
            count=pstmt.executeUpdate();
        }catch (SQLException ex){
            ex.printStackTrace();
        }finally {
            closeAll(conn,pstmt,rs);
        }
        return count;
    }
}
